package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.task.UserTaskActive;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserTaskActiveDaoImpl extends TempDataObjectDao<UserTaskActive> {

	public UserTaskActive getUserTaskActive(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_task_active where userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTaskActive data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserTaskActive data) {
		PreparedStatement call = null;
		String sql = "insert into t_u_task_active(userId,taskActive,taskGetRewardIndexList,lastUpdateTime,sevenDayStartTime,sevenDayActive,"
				+ "sevenDayGetRewardIndexList) values(?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getTaskActive());
			call.setString(i++, getTaskGetRewardIndex(data.getTaskGetRewardIndexList()));
			call.setLong(i++, data.getLastUpdateTime());
			call.setLong(i++, data.getSevenDayStartTime());
			call.setInt(i++, data.getSevenDayActive());
			call.setString(i++, getTaskGetRewardIndex(data.getSevenDayGetRewardIndexList()));
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserTaskActive data) {
		PreparedStatement call = null;
		String sql = "update t_u_task_active set taskActive=?,taskGetRewardIndexList=?,lastUpdateTime=?,sevenDayStartTime=?,sevenDayActive=?,"
				+ "sevenDayGetRewardIndexList=? where userId=? ";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getTaskActive());
			call.setString(i++, getTaskGetRewardIndex(data.getTaskGetRewardIndexList()));
			call.setLong(i++, data.getLastUpdateTime());
			call.setLong(i++, data.getSevenDayStartTime());
			call.setInt(i++, data.getSevenDayActive());
			call.setString(i++, getTaskGetRewardIndex(data.getSevenDayGetRewardIndexList()));
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	private String getTaskGetRewardIndex(List<Integer> list) {

		String taskGetRewardIndex = "";
		if (list == null || list.size() == 0) {
			return taskGetRewardIndex;
		}

		for (Integer integer : list) {
			if (!"".equals(taskGetRewardIndex)) {
				taskGetRewardIndex += ",";
			}
			taskGetRewardIndex += integer;
		}
		return taskGetRewardIndex;
	}

	private List<Integer> getTaskGetRewardIndex(String taskGetRewardIndex) {
		List<Integer> indexArr = new ArrayList<Integer>();

		if (!StringUtils.isNullOrEmpty(taskGetRewardIndex)) {
			String[] arr = taskGetRewardIndex.split(",");
			if (arr != null) {
				for (String index : arr) {
					indexArr.add(Integer.parseInt(index.trim()));
				}
			}
		}
		return indexArr;
	}

	@Override
	protected UserTaskActive parseData(ResultSet rs) throws SQLException {
		UserTaskActive data = new UserTaskActive();
		data.setUserId(rs.getLong("userId"));
		data.setTaskActive(rs.getInt("taskActive"));
		data.setTaskGetRewardIndexList(getTaskGetRewardIndex(rs.getString("taskGetRewardIndexList")));
		data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
		data.setSevenDayStartTime(rs.getLong("sevenDayStartTime"));
		data.setSevenDayActive(rs.getInt("sevenDayActive"));
		data.setSevenDayGetRewardIndexList(getTaskGetRewardIndex(rs.getString("sevenDayGetRewardIndexList")));
		return data;
	}
}
